<?php
/***************************************************************************
   Coppermine 1.3.1 for CPG-Dragonfly™
  **************************************************************************
   Port Copyright (c) 2004-2005 CPG Dev Team
   http://dragonflycms.com/
  **************************************************************************
   v1.1 (c) by Grégory Demar http://coppermine.sf.net/
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
****************************************************************************/
if (!defined('ADMIN_PAGES')) { exit; }
if (!can_admin($op)) { exit; }
require("modules/" . $op . "/include/load.inc");

function form_label($text) {
	echo '<tr><th colspan="2">'.$text.'</th></tr>';
}

function form_input($text, $name) {
	global $CONFIG;
	$value = htmlprepare($CONFIG[$name]);
	echo <<<EOT
	<tr>
		<td width="300">$text</td>
		<td >
			<input type="text" style="width: 90%" name="$name" value="$value" />
		</td>
	</tr>
EOT;
}

function form_yes_no($text, $name) {
	global $CONFIG;
	$no_selected = empty($CONFIG[$name]) ? 'selected="selected"' : '';
	echo '
	<tr>
		<td>'.$text.'</td>
		<td>
			<select name="'.$name.'">
				<option value="1">'.YES.'</option>
				<option value="0" '.$no_selected.'>'.NO.'</option>
			</select>
		</td>
	</tr>
';
}

function form_img_pkg($text, $name) {
	global $CONFIG;
	$value = $CONFIG[$name];
	echo '
	<tr>
		<td>'.$text.'</td>
		<td>
			<select name="'.$name.'">';
	foreach (\Poodle\Image::getHandlers() as $name => $info) {
		echo '<option value="'.$name.'" '.($value == $name ? 'selected="selected"' : '').'>'.$info['name'].'</option>';
	}
	echo '
			</select>
	   </td>
	</tr>';
}

function form_sort_order($text, $name) {
	global $CONFIG, $lang_config_php;
	$value = $CONFIG[$name];
	$ta_selected = ($value == 'ta') ? 'selected="selected"' : '';
	$td_selected = ($value == 'td') ? 'selected="selected"' : '';
	$na_selected = ($value == 'na') ? 'selected="selected"' : '';
	$nd_selected = ($value == 'nd') ? 'selected="selected"' : '';
	$da_selected = ($value == 'da') ? 'selected="selected"' : '';
	$dd_selected = ($value == 'dd') ? 'selected="selectdd"' : '';
	$ra_selected = ($value == 'ra') ? 'selected="selected"' : '';
	$rd_selected = ($value == 'rd') ? 'selected="selected"' : '';
	echo '
	<tr>
		<td>'.$text.'</td>
		<td>
			<select name="'.$name.'">
				<option value="ta" '.$ta_selected.'>'.TITLE_A.'</option>
				<option value="td" '.$td_selected.'>'.TITLE_D.'</option>
				<option value="na" '.$na_selected.'>'.NAME_A.'</option>
				<option value="nd" '.$nd_selected.'>'.NAME_D.'</option>
				<option value="da" '.$da_selected.'>'.DATE_A.'</option>
				<option value="dd" '.$dd_selected.'>'.DATE_D.'</option>
				<option value="ra" '.$ra_selected.'>'.RATING_A.'</option>
				<option value="rd" '.$rd_selected.'>'.RATING_A.'</option>
			</select>
		</td>
	</tr>
	';
}

function form_theme($text, $name) {
	global $CONFIG, $CPG_M_DIR;
	//if ($name == "cpgtheme") $value = $CONFIG["theme"];
	//else
	$value = $CONFIG['theme'];
	$theme_dir = $CPG_M_DIR . '/themes/';
	$dir = opendir($theme_dir);
	while ($file = readdir($dir)) {
		if (is_dir($theme_dir . $file) && $file != "." && $file != ".." && $file != "CVS") {
			$theme_array[] = $file;
		}
	}
	closedir($dir);
	natcasesort($theme_array);
	echo <<<EOT
	<tr>
		<td>$text</td>
		<td>
			<select name="$name">
EOT;
	foreach ($theme_array as $theme) {
		echo "<option value=\"$theme\" " . ($value == $theme ? 'selected="selected"' : '') . ">" . strtr(ucfirst($theme), '_', ' ') . "</option>\n";
	}
	echo <<<EOT
			</select>
		</td>
	</tr>
EOT;
}

// Added for allowing user to select which aspect of thumbnails to scale
function form_scale($text, $name) {
	global $CONFIG, $lang_config_php ;
	$value = $CONFIG[$name];
	$any_selected = ($value == 'max') ? 'selected' : '';
	$ht_selected = ($value == 'ht') ? 'selected' : '';
	$wd_selected = ($value == 'wd') ? 'selected' : '';
	// bug fix scott http://coppermine.sourceforge.net/board/viewtopic.php?t=441
	$any_name = $lang_config_php['th_any'];
	$ht_name = $lang_config_php['th_ht'];
	$wd_name = $lang_config_php['th_wd'];
	echo <<<EOT
	<tr>
		<td>$text</td>
		<td>
			<select name="$name">
				<option value="any" $any_selected>$any_name</option>
				<option value="ht" $ht_selected>$ht_name</option>
				<option value="wd" $wd_selected>$wd_name</option>
			</select>
		</td>
	</tr>
EOT;
}

function create_form(&$data) {
	global $CPG_M_DIR;
	$first = true;
	foreach ($data as $element) {
		if (is_array($element)) {
			switch ($element[2]) {
				case 0 :
					form_input($element[0], $element[1]);
					break;
				case 1 :
					form_yes_no($element[0], $element[1]);
					break;
				case 2 :
					form_img_pkg($element[0], $element[1]);
					break;
				case 3 :
					form_sort_order($element[0], $element[1]);
					break;
				case 4 :
					//form_charset($element[0], $element[1]);
					break;
				case 5 :
					break;
				case 6 :
					form_theme($element[0], $element[1]);
					break;
				// Thumbnail scaling
				case 7 :
					form_scale($element[0], $element[1]);
					break;
				// Add triple dropdown later - put back in for compatibility reasons
				case 8 :
					// do nothing
					break;
				default:
					die('Invalid action');
			} // switch
		} else {
			if (!$first) { echo '</tbody>'; }
			$first = false;
			echo '<tbody>';
			form_label($element);
		}
	}
	if (!$first) { echo '</tbody>'; }
}

if (count($_POST) > 0) {
	$msg = UPD_SUCCESS;
	$redirect = URL::admin();
	if (isset($_POST['update_config'])) {
		$need_to_be_positive = array(
			'albums_per_page',
			'albums_per_page',
			'album_list_cols',
			'max_tabs',
			'picture_width',
			'thumb_width',
			'thumbcols',
			'thumbrows',
			// Show filmstrip
			'max_film_strip_items'
		 );
		foreach ($need_to_be_positive as $parameter)
			$_POST[$parameter] = max(1, intval($_POST[$parameter]));
		$_POST['subcat_level'] = max(0, intval($_POST['subcat_level']));
		foreach($lang_config_data as $element) {
			if ((is_array($element))) {
				if ((!isset($_POST[$element[1]]))) {
					cpg_die(_CRITICAL_ERROR, sprintf(_ERROR_NOT_SET, $element[1]), __FILE__, __LINE__);
				}
				$value = Fix_Quotes($_POST[$element[1]]);
				$db->sql_query("UPDATE {$CONFIG['TABLE_CONFIG']} SET value='$value' WHERE name='{$element[1]}'",false,__FILE__, __LINE__);
			}
		}
	} elseif (isset($_POST['restore_config'])) {
		require_once('includes/classes/sqlctrl.php');
		if (!SQLCtrl::query_file('includes/coppermine/restore_cfg.gz', $error, array('cms_cpg_'=>$cpg_prefix))) {
			cpg_error($error);
		}
		$msg = RESTORE_SUCCESS;
	}
	URL::refresh($redirect);
	pageheader(CONFIG_TITLE,$redirect);
	msg_box(INFO, $msg, CONTINU, URL::admin());
	pagefooter();
}
Dragonfly_Page::setTitle('. '._BC_DELIM.' '.CONFIG_TITLE);
$signature = 'Coppermine Photo Gallery ' . $CPG_VERSION;
require_once('header.php');
OpenTable();
echo '<fieldset style="width:45%;float:left;"><legend> '._coppermineLANG.' '._ADMINISTRATION.' </legend>
<form action="'.URL::admin('', false).'" method="get">';
$result = $db->sql_query("SELECT dirname FROM ".$prefix."_cpg_installs", false, __FILE__, __LINE__);
if ($db->sql_numrows($result) > 1) {
	$multiinstalls = true;
	echo '
	<label for="name">'._W_INSTALL.'</label>
	<select name="name" id="name">';
	while (list($cpg_dir) = $db->sql_fetchrow($result)) {
		echo '<option value="'.$cpg_dir.'">'.$cpg_dir.'</option>';
	}
	echo '
	</select><br />';
} else {
	$multiinstalls = false;
	list($cpg_dir) = $db->sql_fetchrow($result);
	echo '<input type="hidden" name="name" id="name" value="'.$cpg_dir.'" />';
}
$result->free();
echo '
	<label for="file">'._W_PAGE.'</label>
	<select name="file" id="file">
		<option value="albmgr">'.ALBUMS_LNK.'</option>
		<option value="searchnew">'.SEARCHNEW_LNK.'</option>
		<option value="catmgr">'.CATEGORIES_LNK.'</option>
		<option value="groupmgr">'.GROUPS_LNK.'</option>
		<option value="help">'.HELP_TITLE.'</option>
		<option value="reviewcom">'.COMMENTS_LNK.'</option>
		<option value="search">'.SEARCH_LNK.'</option>
		<option value="upload">'.UPLOAD_PIC_LNK.'</option>
		<option value="usermgr">'.USERS_LNK.'</option>
	</select>
	<input type="submit" value="'._GO.'" />
</form></fieldset>';
if ($multiinstalls) {
	reset($cpg_dir);
	echo '<fieldset  style="width:45%;float:left;"><legend> '._coppermineLANG.' '.CONFIG_TITLE.' </legend>
			<form action="'.URL::admin().'" method="post">
				<select name="op" id="op">';
	foreach ($cpg_dir as $cpg_cdir  )  {
			echo '		<option value="'.$cpg_cdir.'">'.$cpg_cdir.' '.CONFIG_LNK.'</option>';
		}
	echo '
			</select>
		<input type="submit" value="'._GO.'" />
	</form></fieldset>';
}
echo '<br clear="all" />

<form action="'.URL::admin().'" method="post" enctype="multipart/form-data">
';
starttable('100%', CONFIG_TITLE." - $signature", 2);
create_form($lang_config_data);
echo '
<tfoot>
	<tr>
		<td colspan="2">
			<button type="submit" name="update_config">'.SAVE_CFG.'</button>
			<button type="submit" name="restore_config">'.RESTORE_CFG.'</button>
		</td>
	</tr>
</tfoot>';
endtable();
echo '</form>';
CloseTable();
